//
//  QYXDailyTableViewCell.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXDailyTableViewCell.h"
#import "UIImageView+QYXCarveImage.h"
#import "UIImageView+WebCache.h"
@implementation QYXDailyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.clipsToBounds = YES;
        
        _mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -(ScreenHeight/1.7 -250 *FitHeight)/2, ScreenWidth, ScreenHeight/1.7)];
        
//        _mainImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView  addSubview:_mainImageView];
        
        self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 250*FitHeight)];
        self.blackView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.33];
        [self.contentView addSubview:self.blackView];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 250*FitHeight / 2 - 50, ScreenWidth-60, 60)];
        
        _titleLabel.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:17];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.numberOfLines = 2;
        [self.contentView addSubview:_titleLabel];
        
        self.activeTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 250*FitHeight / 2 + 30, ScreenWidth, 30)];
        self.activeTimeLabel.font = [UIFont systemFontOfSize:14];
        self.activeTimeLabel.textAlignment = NSTextAlignmentCenter;
        self.activeTimeLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.activeTimeLabel];

       //初始化全景label
        self.fullViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 140, 20, 120, 20)];
        self.fullViewLabel.textColor = [UIColor whiteColor];
        self.fullViewLabel.textAlignment = 2;
        self.fullViewLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_fullViewLabel];
        
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
        [self.contentView addGestureRecognizer:longGesture];

    }
    return self;
}
- (void)longTap:(UILongPressGestureRecognizer *)sender
{
    self.titleLabel.textColor = [UIColor colorWithWhite:1.000 alpha:0.000];
    self.activeTimeLabel.textColor = [UIColor colorWithWhite:1.000 alpha:0.000];
    self.fullViewLabel.textColor = [UIColor colorWithWhite:1.000 alpha:0.000];
    self.blackView.backgroundColor = [UIColor clearColor];
    if (sender.state == UIGestureRecognizerStateEnded) {
        self.blackView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.activeTimeLabel.textColor = [UIColor whiteColor];
        self.fullViewLabel.textColor = [UIColor whiteColor];
    }
    
}
#pragma mark -- 尺寸大小


- (CGFloat)cellOffset {
    
    CGRect centerToWindow = [self convertRect:self.bounds toView:self.window];
    CGFloat centerY = CGRectGetMidY(centerToWindow);
    CGPoint windowCenter = self.superview.center;
    
    CGFloat cellOffsetY = centerY - windowCenter.y;
    
    CGFloat offsetDig =  cellOffsetY / self.superview.frame.size.height *2;
    CGFloat offset =  -offsetDig * (ScreenHeight /1.7 - 250 *FitHeight)/2;
    
    CGAffineTransform transY = CGAffineTransformMakeTranslation(0,offset);

    self.mainImageView.transform = transY;

    
    return offset;
}


#pragma mark -- model
- (void)setModel:(DYJMovieListModel *)model
{
    if (_model != model) {
            _model = model;
            if (_model.img_url) {
                [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:_model.img_url] placeholderImage:[UIImage imageNamed:@"xph"]];
            }
            self.titleLabel.text = [NSString stringWithFormat:@"%@", _model.title];
            self.activeTimeLabel.text = [NSString stringWithFormat:@"#%@  /  喜欢:%@", _model.subtitle, _model.like_count];
        }
}

- (void)setPreviewingDelegate:(id<UIViewControllerPreviewingDelegate>)previewingDelegate{
    // 这里设置代理,需要控制好设置(registerForPreviewingWithDelegate)的次数,确保每个cell都有register就可以,但不要重复(若大量registerForPreviewingWithDelegate 会造成滑动失控)
    if (!_previewingDelegate){
        _previewingDelegate = previewingDelegate;
        UIViewController *controller = (UIViewController *)previewingDelegate;
        [controller registerForPreviewingWithDelegate:previewingDelegate sourceView:self.contentView];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
